package com.linkage.event.service.impl;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.linkage.common.exception.ServiceException;
import com.linkage.event.pojo.dao.EventTypeMapper;
import com.linkage.event.pojo.entity.EventType;
import com.linkage.event.pojo.vo.EventTypeVo;
import com.linkage.event.service.EventTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Struct;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class EventTypeServiceImpl implements EventTypeService {
    @Resource
    private EventTypeMapper eventTypeMapper;

    @Override
    public List<EventTypeVo> getEventTypeList(String type, String status) {
        List<EventType> superList = eventTypeMapper.selectAllSuperiorType(type,status);
        List<EventTypeVo> resultList = new ArrayList<>();
        for(EventType eventType:superList){
            List<EventTypeVo> childListVo = new ArrayList<>();
            //循环插入子目录
            List<EventType> childList = eventTypeMapper.selectBySuperiorTypeCode(eventType.getEventTypeCode(),status);
            childList.forEach(x -> childListVo.add(EventTypeVo.builder().
                    eventTypeName(x.getEventTypeName()).eventTypeCode(x.getEventTypeCode()).build()));
            EventTypeVo typeVo = EventTypeVo.builder().eventTypeCode(eventType.getEventTypeCode())
                    .enableStatus(eventType.getEnableStatus()).createTime(eventType.getCreateTime())
                    .eventTypeName(eventType.getEventTypeName()).childTypeCode(childListVo).isDefault(eventType.getIsDefault()).build();
            if(StrUtil.equals("1",type)){
                typeVo.setIcon("https://jbdd.zgzhjb.cn/icon.png");
            }
            resultList.add(typeVo);
        }
        return resultList;
    }

    @Override
    public void insert(EventType eventType) {
        eventType.setEventTypeCode(UUID.fastUUID().toString(true));
        eventType.setCreateTime(new Date());
        eventTypeMapper.insertSelective(eventType);
    }

    @Override
    public void update(EventType eventType) {
        if(StrUtil.isEmpty(eventType.getEventTypeCode())){
            throw new ServiceException(500,"eventTypeCode参数为空");
        }
        eventTypeMapper.updateByPrimaryKeySelective(eventType);
    }

    @Override
    public void delete(String code) {
        if(StrUtil.isNotEmpty(code)){
            eventTypeMapper.deleteByPrimaryKey(code);
        }
    }

}
